package com.hy.mydesktop.client.component.factory;

import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.DesktopMenuItemModelEnum;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-17；时间：上午09:39:37</li>
 * <li>类型名称：DesktopMenuItemFactory</li>
 * <li>设计目的：Desktop的开始菜单下的MenuItem工厂</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class DesktopStartMenuFactory {
	/**
	 * 
	 * <ul>
	 * <li>方法含义：</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-17；时间：下午上午09:42:03</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param desktop
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	public static Desktop createStartMenu(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		return configureStartMenu(desktop,gxtComponentMetaNodeModel);
		
		
	}
	
	public static MenuItem createStartMenuToDesktop(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		MenuItem menuItem  = createDefaultStartMenu();
		menuItem = AbstractComponetFactory.configureAbstractComponet(menuItem, gxtComponentMetaNodeModel);

		
		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			menuItem.setText((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ID.name().toLowerCase())!=null) {
			
			menuItem.setId((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase())!=null) {
			
			menuItem.setTitle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase())!=null) {
			
			menuItem.setIconStyle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
					
			final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 5://Integer.valueOf("0005")
		            	SelectionListener<MenuEvent> shortcutListener = new SelectionListener<MenuEvent>() {
						      @Override
						      public void componentSelected(MenuEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  
						    	  
						    	  Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
						    	  EventStructureMetaModel eventstructuremetamodel = 
										 (EventStructureMetaModel)data.get("eventstructuremetamodel");
						    	  System.out.println("MENUITEM触发AppEvents.DESKTOP_STARTMENU_ONCLICK_INIT事件:"
						    			  +eventstructuremetamodel.getControllerId()
						    			  +eventstructuremetamodel.getResponseModeId());
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_STARTMENU_ONCLICK_INIT,data);
						    	  
					    }};
					    /**
						 * 桌面快捷方式的监听器
						 */
					    menuItem.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}		
		
		}
		if(desktop.getTaskBar().getStartMenu().getHeading()==null){
			desktop.getTaskBar().getStartMenu().setHeading("dc");
		}
		desktop.getTaskBar().getStartMenu().addTool(menuItem);
		
		 //StartMenu startMenu = taskBar.getStartMenu();
		 //startMenu.add(menuItem);
	
		return menuItem;
		
		
	}
	
	private static Desktop configureStartMenu(final Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		MenuItem menuItem  = createDefaultStartMenu();
	
		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			menuItem.setText((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase())!=null) {
			
			menuItem.setTitle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase())!=null) {
			
			menuItem.setIconStyle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
					
			final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 5://Integer.valueOf("0005")
		            	SelectionListener<MenuEvent> shortcutListener = new SelectionListener<MenuEvent>() {
						      @Override
						      public void componentSelected(MenuEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  Object data = appEventCodes.get(code);
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_STARTMENU_ONCLICK_INIT,data);
								 
					    }};
					    /**
						 * 桌面快捷方式的监听器
						 */
					    menuItem.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}		
		
		}
		if(desktop.getTaskBar().getStartMenu().getHeading()==null){
			desktop.getTaskBar().getStartMenu().setHeading("dc");
		}
		desktop.getTaskBar().getStartMenu().addTool(menuItem);
		 //StartMenu startMenu = taskBar.getStartMenu();
		 //startMenu.add(menuItem);
	
		return desktop;
		
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-15；时间：下午上午11:05:29</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static MenuItem createDefaultStartMenu() {
		
		
		 MenuItem menuItem = new MenuItem();
		 menuItem.setText("accordinWindow");
		 menuItem.setTitle("dd");			 
		 menuItem.setIcon(IconHelper.createStyle("accordion"));
		
		 return menuItem;
	}
}
